package com.example.springcloud.controller;

import com.example.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author weiHuang.zhu
 * @Date 21/4/2023 下午8:20
 */
@RestController

public class PaymentController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo(@PathVariable("id")Integer id){
        return paymentHystrixService.paymentInfo(id);
    }

    @HystrixCommand(fallbackMethod = "paymentTimeoutHandler",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_timeout(@PathVariable("id")Integer id){
        return paymentHystrixService.paymentInfo_timeout(id);
    }

    public String paymentTimeoutHandler(Integer id) {
        return Thread.currentThread().getName() + "服务器繁忙！";
    }

}
